package com.itheima.service;

import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hg
 * @date ：Created in 2021/12/22 19:44
 * @description：
 */
public class SpringSecurityService2 implements UserDetailsService {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    //根据用户名查询数据库来获得用户的信息(包含数据库中的用户的密码)，这里是模拟查询
    public static Map<String, User> map = new HashMap<>();

    public void initUserData(){
        User user1 = new User();
        user1.setUsername("admin");
        user1.setPassword(passwordEncoder.encode("admin"));//使用bcrypt对密码进行加密

        User user2 = new User();
        user2.setUsername("xiaoming");
        user2.setPassword(passwordEncoder.encode("1234"));

        map.put(user1.getUsername(), user1);
        map.put(user2.getUsername(), user2);
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        initUserData();

        System.out.println("用户输入的用户名为：" + username);

        //根据用户名查询数据库来获得用户的信息(包含数据库中的用户的密码)，这里是模拟查询
        User user = map.get(username);
        String password =user.getPassword();

        if (null == user) {
            return null;
        } else {
            //将用户信息进行返回，
            //框架会进行密码比对，页面提交的密码会和数据库中查询的密码进行比对
            List<GrantedAuthority> list = new ArrayList<>();
            //为当前登录用户授权，后期需要改为从数据库查询当前用户对应的权限
            list.add(new SimpleGrantedAuthority("permission_A"));
            list.add(new SimpleGrantedAuthority("permission_B"));


            if (username.equals("admin")) {
                list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
                list.add(new SimpleGrantedAuthority("add"));
            }
            UserDetails userDetails = new org.springframework.security.core.userdetails.User(username, password, list);
            return userDetails;
        }

    }
}
